package syslt

var (
	option = &Option{
		MaxCreateCodes: 10,
	}
)

func NewSysLt(factory Factory, creator Creator) *SysLt {
	return &SysLt{factory: factory, creator: creator, option: option}
}

type (
	Creator interface {
		GenerateCode() (code string)
	}
	Factory interface {
		Count() int64
		Append([]string) (code string, err error)
	}
	Option struct {
		//最大创建数
		MaxCreateCodes int
	}

	SysLt struct {
		factory Factory
		creator Creator
		option  *Option
	}
)

func (s *SysLt) GetCode() (code string, err error) {
	var createNums = 1
	count := s.factory.Count()
	switch {
	case int(count) > s.option.MaxCreateCodes:
		createNums = 0
	case int(count) == 0:
		createNums = s.option.MaxCreateCodes
	default:
		createNums = s.option.MaxCreateCodes - int(count) + 1
	}
	sl := make([]string, 0, createNums)
	for a := 0; a < createNums; a++ {
		sl = append(sl, s.creator.GenerateCode())
	}
	return s.factory.Append(sl)
}

func (s *SysLt) SetOption(op *Option) {
	s.option = op
}
